#import "Equations.h"

@implementation Equations

@synthesize eqn1;
@synthesize eqn2;
@synthesize eqn3;

int answer;

-(NSString*)createNumEqns{
	/* Randomly generate number-type equations (of the format: 2 + 3 = [ ]) with random numbers */
	int questionType = (arc4random()%(3));
	int fromNumber = 1;
	int toNumber = 9;
	
	if (questionType == 0){			
		// equations of the type: 2 + 3 = [ ] 	
		int randomNumber1 = (arc4random()%(toNumber-fromNumber))+fromNumber;
		int randomNumber2 = (arc4random()%(toNumber-fromNumber))+fromNumber;
		int randomNumberForOperand = (arc4random()%(4));
		NSString* operand;
		if (randomNumberForOperand == 0){
			operand = @" + ";
			answer = randomNumber1 + randomNumber2;
		}
		else if (randomNumberForOperand == 1){
			operand = @" - ";
			while (randomNumber1 < randomNumber2){
				randomNumber1 = (arc4random()%(toNumber-fromNumber))+fromNumber;
				randomNumber2 = (arc4random()%(toNumber-fromNumber))+fromNumber;
			}
			answer = randomNumber1 - randomNumber2;
		}
		else if (randomNumberForOperand == 2){
			operand = @" × ";
			answer = randomNumber1 * randomNumber2;
		}
		else {
			operand = @" ÷ ";
			while (randomNumber2 == 0){
				randomNumber2 = (arc4random()%(toNumber-fromNumber))+fromNumber;
			}
			int temp = randomNumber1 * randomNumber2;
			answer = randomNumber1;
			randomNumber1 = temp;
		}
		NSString* temporVar = [[NSString stringWithFormat:@"%d", randomNumber1] stringByAppendingString: operand];
		NSString* temporVar2 = [[NSString stringWithFormat:@"%d", randomNumber2] stringByAppendingString:@" = "];
		NSString* temporVar3 = [temporVar stringByAppendingString: temporVar2];
		NSString* eqnNum = [temporVar3 stringByAppendingString: @" [  ]"];
		return eqnNum;
	}else if (questionType == 1){		
		//equations of the type: 2 + [ ] = 5
		int randomNumber1 = (arc4random()%(toNumber-fromNumber))+fromNumber;
		answer = (arc4random()%(toNumber-fromNumber))+fromNumber;
		int randomNumberForOperand = (arc4random()%(4));
		NSString* operand;
		int randomNumber2;
		if (randomNumberForOperand == 0){
			operand = @" + ";
			randomNumber2 = randomNumber1 + answer;
		}
		else if (randomNumberForOperand == 1){
			operand = @" - ";
			while (randomNumber1 < answer){
				randomNumber1 = (arc4random()%(toNumber-fromNumber))+fromNumber;
				answer = (arc4random()%(toNumber-fromNumber))+fromNumber;
			}
			randomNumber2 = randomNumber1 - answer;
		}
		else if (randomNumberForOperand == 2){
			operand = @" × ";
			while ((randomNumber1 == 0) || (randomNumber2 == 0)){
				randomNumber1 = (arc4random()%(toNumber-fromNumber))+fromNumber;
				randomNumber2 = (arc4random()%(toNumber-fromNumber))+fromNumber;
			}
			randomNumber2 = randomNumber1 * answer;
		}
		else {
			operand = @" ÷ ";
			while ((randomNumber1 == 0) || (randomNumber2 == 0)){
				randomNumber1 = (arc4random()%(toNumber-fromNumber))+fromNumber;
				randomNumber2 = (arc4random()%(toNumber-fromNumber))+fromNumber;
			}
			int temp = randomNumber1 * answer;
			randomNumber2 = randomNumber1;
			randomNumber1 = temp;
		}
		NSString* temporVar1 = [[NSString stringWithFormat:@"%d", randomNumber1] stringByAppendingString: operand];
		NSString* temporVar2 = [@"[  ]" stringByAppendingString:@" = "];
		NSString* temporVar3 = [temporVar1 stringByAppendingString: temporVar2];
		NSString* eqnNum = [temporVar3 stringByAppendingString: [NSString stringWithFormat:@"%d", randomNumber2]];
		return eqnNum;
	}else{								
		//equations of the type [ ] + 3 = 5
		answer = (arc4random()%(toNumber-fromNumber))+fromNumber;
		int randomNumber1 = (arc4random()%(toNumber-fromNumber))+fromNumber;
		int randomNumberForOperand = (arc4random()%(4));
		int randomNumber2;
		NSString* operand;
		if (randomNumberForOperand == 0){
			operand = @" + ";
			randomNumber2 = answer + randomNumber1;
		}
		else if (randomNumberForOperand == 1){
			operand = @" - ";
			while (answer < randomNumber1){
				answer = (arc4random()%(toNumber-fromNumber))+fromNumber;
				randomNumber1 = (arc4random()%(toNumber-fromNumber))+fromNumber;
			}
			randomNumber2 = answer - randomNumber1;
		}
		else if (randomNumberForOperand == 2){
			operand = @" × ";
			while ((randomNumber1 == 0) || (randomNumber2 == 0)){
				randomNumber1 = (arc4random()%(toNumber-fromNumber))+fromNumber;
				randomNumber2 = (arc4random()%(toNumber-fromNumber))+fromNumber;
			}
			randomNumber2 = answer * randomNumber1;
		}
		else {
			operand = @" ÷ ";
			while ((randomNumber1 == 0) || (randomNumber2 == 0)){
				randomNumber1 = (arc4random()%(toNumber-fromNumber))+fromNumber;
				randomNumber2 = (arc4random()%(toNumber-fromNumber))+fromNumber;
			}
			int temp  = answer * randomNumber1;
			randomNumber2 = answer;
			answer = temp;
		}
		NSString* temporVar1 = [@"[  ]" stringByAppendingString: operand];
		NSString* temporVar2 = [[NSString stringWithFormat:@"%d", randomNumber1] stringByAppendingString:@" = "];
		NSString* temporVar3 = [temporVar1 stringByAppendingString: temporVar2];
		NSString* eqnNum = [temporVar3 stringByAppendingString: [NSString stringWithFormat:@"%d", randomNumber2]];		
		return eqnNum;
	}
	
}


-(NSString*)createOperEqns{
	/* Randomly generate operand-type equations (of the format: 2 [ ] 3 = 5) with random numbers */
	int fromNumber = 1;
	int toNumber = 9;
	int randomNumber1 = (arc4random()%(toNumber-fromNumber))+fromNumber;
	int randomNumber2 = (arc4random()%(toNumber-fromNumber))+fromNumber;
	int randomNumber3;
	
	int randomNumberForOperand = (arc4random()%(4));
	if (randomNumberForOperand == 0){
		answer = -1; // = @"+";
		while ((randomNumber1 == 2) && (randomNumber2 == 2)){
			randomNumber1 = (arc4random()%(toNumber-fromNumber))+fromNumber;
			randomNumber2 = (arc4random()%(toNumber-fromNumber))+fromNumber;
		}
		randomNumber3 = randomNumber1 + randomNumber2;
	}
	else if (randomNumberForOperand == 1){
		answer = -2; // = @"-";
		while (randomNumber1 < randomNumber2){
			randomNumber1 = (arc4random()%(toNumber-fromNumber))+fromNumber;
			randomNumber2 = (arc4random()%(toNumber-fromNumber))+fromNumber;
		}
		randomNumber3 = randomNumber1 - randomNumber2;
	}
	else if (randomNumberForOperand == 2){
		answer = -3; // = @"*";
		while ((randomNumber1 == 0) || (randomNumber2 == 0)){
			randomNumber1 = (arc4random()%(toNumber-fromNumber))+fromNumber;
			randomNumber2 = (arc4random()%(toNumber-fromNumber))+fromNumber;
		}
		while (randomNumber2 == 1){
			randomNumber2 = (arc4random()%(toNumber-fromNumber))+fromNumber;
		}
		while ((randomNumber1 == randomNumber2)){
			randomNumber1 = (arc4random()%(toNumber-fromNumber))+fromNumber;
			randomNumber2 = (arc4random()%(toNumber-fromNumber))+fromNumber;
		}
		randomNumber3 = randomNumber1 * randomNumber2;
	}
	else {
		answer = -4; // = @"/";
		while (randomNumber2 == 1){
			randomNumber2 = (arc4random()%(toNumber-fromNumber))+fromNumber;
		}
		while ((randomNumber1 == 0) || (randomNumber2 == 0)){
			randomNumber1 = (arc4random()%(toNumber-fromNumber))+fromNumber;
			randomNumber2 = (arc4random()%(toNumber-fromNumber))+fromNumber;
		}
		while ((randomNumber1 == randomNumber2)){
			randomNumber1 = (arc4random()%(toNumber-fromNumber))+fromNumber;
			randomNumber2 = (arc4random()%(toNumber-fromNumber))+fromNumber;
		}
		int temp = randomNumber1 * randomNumber2;
		randomNumber3 = randomNumber1;
		randomNumber1 = temp;
	}
	//
	NSString* temporVar = [[NSString stringWithFormat:@"%d", randomNumber1] stringByAppendingString: @" [   ] "];
	NSString* temporVar2 = [[NSString stringWithFormat:@"%d", randomNumber2] stringByAppendingString: @" = "];
	NSString* temporVar3 = [temporVar stringByAppendingString: temporVar2];
	NSString* eqnOper = [temporVar3 stringByAppendingString: [NSString stringWithFormat:@"%d", randomNumber3]];
	return eqnOper;
}


-(int)answerToEqns{
	/* return answer to the current equation*/
	return answer;
}


-init{

	if (self = [super init])
    {
		eqn1 = [self createNumEqns];
		eqn2 = [self createNumEqns];
		eqn3 = [self createNumEqns];
	}
	
	return self;
}

@end
